<?php

namespace App\Middleware\ws;

use App\Constants\ErrorCode;
use App\Model\Admin\Admin;
use App\Repository\WebSocket\UsersData;
use App\Service\Users\UsersService;
use Firebase\JWT\JWT;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use App\Model\User;
use Hyperf\HttpServer\Contract\ResponseInterface as Response;
use Hyperf\Di\Annotation\Inject;
use Hyperf\WebSocketServer\Context;


class WebSocketAuthMiddleware implements MiddlewareInterface
{
    /**
     * @Inject
     * @var Response
     */
    protected $response;

    /**
     * @Inject()
     * @var Admin
     */
    protected $adminModel;


    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {

//        if (Context::has('uid')) {
//            return $handler->handle($request);
//        }
        dump(111);
        $token = $request->getQueryParams()['token'] ?? '';
        $form = $request->getQueryParams()['from'] ?? 'minapp';
//        dump($token, $request->getUri(), $form);

        $UsersData = app(UsersData::class)->instance($token, $request->getUri(), $form);
        dump($UsersData, $request->getUri(), $form);


        if (!$token) {
            //用户未登录
            return $this->response->json(err('用户未登录', ErrorCode::TOEKN_NOT_EXISTS));
        }

        Context::set('userData', $UsersData);
//        dump($UsersData);
//        dump(1);
//        dump(Context::get('userData'));

        return $handler->handle($request);
    }

}